﻿using System;
using Nop.Core;
using Nop.Core.Domain.Tax;
using System.Collections.Generic;
using Nop.Core.Data;
using Nop.Core.Caching;
using System.Linq;

namespace Nop.Services.Tax
{
    public partial class SurchargeService : ISurchargeService
    {
        #region Constants
        private const string SURCHARGETYPE_ALL_KEY = "Spotshub.surchargetype.all";
        private const string SURCHARGE_PATTERN_KEY = "Spotshub.surcharge.";
        private const string SURCHARGE_BY_ID_KEY = "Spotshub.surcharge.id-{0}";
        #endregion

        #region Fields

        private readonly IRepository<Surcharge> _surchargeRepository;
        private readonly IRepository<SurchargeType> _surchargeTypeRepository;
        private readonly ICacheManager _cacheManager;

        #endregion

        public SurchargeService(
            IRepository<Surcharge> surchargeRepository,
            IRepository<SurchargeType> surchargeTypeRepository,
            ICacheManager cacheManager
            )
        {
            this._surchargeRepository = surchargeRepository;
            this._surchargeTypeRepository = surchargeTypeRepository;
            this._cacheManager = cacheManager;
        }

        #region Methods

        public virtual IList<SurchargeType> GetAllSurchargeTypes()
        {
            string key = String.Format(SURCHARGETYPE_ALL_KEY);
            return _cacheManager.Get(key, () =>
            {
                var query = from st in _surchargeTypeRepository.Table
                            orderby st.DisplayOrder
                            select st;
                return query.ToList();
            });
        }

        public virtual void DeleteSurcharge(Surcharge surcharge)
        {
            if (surcharge == null)
                throw new ArgumentNullException("surcharge");

            _surchargeRepository.Delete(surcharge);

            _cacheManager.RemoveByPattern(SURCHARGE_PATTERN_KEY);
        }

        public virtual IPagedList<Surcharge> GetAllSurcharges(int pageIndex = 0, int pageSize = int.MaxValue)
        {
            throw new System.NotImplementedException();
        }

        public virtual IList<Surcharge> GetAllSurcharges(int taxCategoryId, int? countryId, int? stateProvinceId, string zip)
        {
            throw new System.NotImplementedException();
        }

        public virtual Surcharge GetSurchargeById(int surchargeId)
        {
            if (surchargeId == 0)
                return null;

            string key = string.Format(SURCHARGE_BY_ID_KEY, surchargeId);
            return _cacheManager.Get(key, () =>
            {
                var surcharge = _surchargeRepository.GetById(surchargeId);
                return surcharge;
            }); 
        }

        public virtual void InsertSurcharge(Surcharge surcharge)
        {
            throw new System.NotImplementedException();
        }

        public virtual void UpdateSurcharge(Surcharge surcharge)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }
}
